package com.develop.web.in.request.json.field.deserializer;

import com.develop.data.loan.option.LoanRateType;
import com.develop.exception.BusinessException;
import com.develop.exception.BusinessExceptionInfo;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
//@JsonComponent
public class Json2LoanRateType extends StdDeserializer<LoanRateType> {
    protected Json2LoanRateType() {
        super(LoanRateType.class);
    }

    @Override
    public LoanRateType deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {

        //String foo = p.getValueAsString();
        //Optional<LoanRateType> bar = Arrays.stream(LoanRateType.values()).filter(item -> foo.equals(item.getCode())).findFirst();
        //bar.orElseThrow(()-> new BusinessException(BusinessExceptionInfo.PARAMETER ));
        //return bar.get();

        String foo = p.getValueAsString();
        for(LoanRateType bar: LoanRateType.values()){
            if( foo.equals(bar.getCode()) ) return bar;
        }
        throw new BusinessException(BusinessExceptionInfo.PARAMETER);
    }
}
